function request(url, method, params, success, fail, formatParams) {
	params = params || "";
	var uinfoJson = localStorage.getItem(globalData.baseUrl + 'uinfo');
	var uinfo = uinfoJson ? JSON.parse(uinfoJson) : {};
	$('#loading').show();
	var opts = {
		method: method,
		url: url,
		headers: {
			skey: uinfo.skey || "",
			ucode: uinfo.ucode || ""
		},
		success: function (resp, _status, _xhr) {
			$('#loading').hide();
			if (resp.r && resp.code >= 200 && resp.code < 300) {
				success && success(resp);
			} else {
				var code = resp.code || _xhr.status;
				if (code === 401 || code === 403) {
					top.location.href = globalData.loginUrl;
				} else {
					fail && fail(resp.m || "操作失败，请检查参数或稍后再试", _xhr);
				}
			}
		},
		error: function (_xhr) {
			$('#loading').hide();
			var code = _xhr.responseJSON && _xhr.responseJSON.code || _xhr.status;
			if (code === 401 || code === 403) {
				top.location.href = globalData.loginUrl;
			} else {
				if (_xhr.responseJSON) {
					fail && fail(_xhr.responseJSON.m || "操作失败，请检查参数或稍后再试", _xhr);
				} else {
					fail && fail(_xhr.responseText, _xhr);
				}
			}
		}
	};
	if (formatParams) {
		opts.data = params;
		opts.processData = true;//发送的数据将被转换为对象(技术上讲并非字符串) 以配合默认内容类型 "application/x-www-form-urlencoded"
	} else {
		opts.data = JSON.stringify(params);
		opts.processData = false;
		opts.dataType = 'json';
		opts.contentType = 'application/json;charset=UTF-8';
	}
	jQuery.ajax(opts);
}

var utils = {};
utils.confirm = function (msg, yesCallback, noCallback, options) {
	if (layer && layer.confirm) {
		var opts = {
			title: false,
			btn: ['确定', '取消'],
			closeBtn: 0,
			icon: 3
		};
		if (options) {
			opts = Object.assign({}, opts, options);
		}
		layer.confirm(msg || "确定执行该操作吗", opts, function (index) {
			layer.close(index);
			yesCallback && yesCallback();
		}, function (index) {
			layer.close(index);
			noCallback && noCallback();
		});
	} else {
		if (confirm(msg)) {
			yesCallback && yesCallback();
		} else {
			noCallback && noCallback();
		}
	}
};
utils.alert = function (msg, callback, options) {
	if (layer && layer.msg) {
		var opts = {
			//title: false,
			//btnAlign: 'c',
			time: 3500,
			//closeBtn: 0,
			icon: 0
		};
		if (options) {
			opts = Object.assign({}, opts, options);
		}
		//layer.alert(msg, opts, function (index) {
		//	layer.close(index);
		//	callback && callback();
		//});
		layer.msg(msg, opts, function () {
			callback && callback();
		});
	} else {
		alert(msg);
		callback && callback();
	}
};
utils.pageStore = function (data, pageNum, pageSize) {
	data = data || {};
	if (data.pnum) {
		data.pnum = pageNum || data.pnum;
	}
	if (data.psize) {
		data.psize = pageSize || data.psize;
	}
	localStorage.setItem(globalData.baseUrl + "pathdata", JSON.stringify({ path: location.pathname, data: data }));
};
utils.pageRestore = function () {
	var dataJson = localStorage.getItem(globalData.baseUrl + 'pathdata');
	var data = dataJson ? JSON.parse(dataJson) : {};
	if (location.pathname === data.path) {
		return data.data;
	} else {
		return {};
	}
};
utils.pageReset = function () {
	localStorage.removeItem(globalData.baseUrl + "pathdata");
};
utils.success = function (msg, callback, options) {
	if (layer && layer.msg) {
		var opts = {
			time: 2000,
			icon: 1
		};
		if (options) {
			opts = Object.assign({}, opts, options);
		}
		layer.msg(msg, opts, function () {
			callback && callback();
		});
	} else {
		alert(msg);
		callback && callback();
	}
};
utils.ajax = request;
utils.ajaxGet = function (url, params, success, fail) {
	request(url, 'GET', params, success, fail, true);
};
utils.ajaxPost = function (url, params, success, fail, formatParams) {
	request(url, 'POST', params, success, fail, formatParams);
};
utils.ajaxExport = function (url, parasJson) {
	var $form = $('<form method="POST" action="' + url + '" target="">');
	var uinfoJson = localStorage.getItem(globalData.baseUrl + 'uinfo');
	var uinfo = uinfoJson ? JSON.parse(uinfoJson) : {};
	$form.append($('<input type="hidden" name="_skey" value="' + (uinfo.skey || "") + '" />'));
	$form.append($('<input type="hidden" name="_ucode" value="' + (uinfo.ucode || "") + '" />'));
	var i, j;
	for (i in parasJson) {
		if (parasJson.hasOwnProperty(i)) {
			if (parasJson[i] instanceof Array) {
				for (j = 0; j < parasJson[i].length; j++) {
					$form.append($('<input type="hidden" name="' + i + '[]" value="' + parasJson[i][j] + '" />'));
				}
			} else {
				$form.append($('<input type="hidden" name="' + i + '" value="' + parasJson[i] + '" />'));
			}
		}
	}
	$('body').append($form);
	$form.submit();
	$form.remove();
};
/**
 * 对日期进行格式化，
 * @param {日期或字符串} date 要格式化的日期
 * @param {字符串} format 进行格式化的模式字符串yyyy-MM-dd hh:mm:ss,SSS,q季度
 * @returns {String} 结果
 */
utils.formatDate = function (date, format) {
	if (!date) {
		return "";
	}
	if (typeof date === "string") {
		if (/^0001[\-\/]/.test(date)) {
			return "";
		}
		var mts = date.match(/(\/Date\((\d+)\)\/)/);
		if (mts && mts.length >= 3) {
			date = parseInt(mts[2]);
		}
		date = new Date(date.replace(/-/g, '/'));//修复IE浏览器问题
	}
	format = format || "yyyy-MM-dd";
	if (!date || date.toUTCString() === "Invalid Date") {
		return "";
	}
	var map = {
		"M": date.getMonth() + 1, //月份
		"d": date.getDate(), //日
		"h": date.getHours(), //小时
		"m": date.getMinutes(), //分
		"s": date.getSeconds(), //秒
		"q": Math.floor((date.getMonth() + 3) / 3), //季度
		"S": date.getMilliseconds() //毫秒
	};
	format = format.replace(/([yMdhmsqS])+/g, function (all, t) {
		var v = map[t];
		if (v !== undefined) {
			if (all.length > 1) {
				v = '0' + v;
				v = v.substr(v.length - 2);
			}
			return v;
		}
		else if (t === 'y') {
			return (date.getFullYear() + '').substr(4 - all.length);
		}
		return all;
	});
	return format;
};

/**
 * 对日期进行格式化，
 * @param {数值} ticks 要格式化的日期时间戳
 * @param {字符串} format 进行格式化的模式字符串yyyy-MM-dd hh:mm:ss,SSS,q季度
 * @returns {String} 结果
 */
utils.formatTicks = function (ticks, format) {
	if (!ticks || ticks <= 621355968000000000) {
		return ticks;
	}
	var date = new Date(parseInt(ticks / 10000) - 62135596800000);
	return utils.formatDate(date, format);
};

/**
 * 对日期进行格式化，
 * @param {数值} ticks 要格式化的日期时间戳
 * @param {字符串} format 进行格式化的模式字符串yyyy-MM-dd hh:mm:ss,SSS,q季度
 * @returns {String} 结果
 */
utils.formatUTCTicks = function (ticks, format) {
	if (!ticks || ticks <= 621356256000000000) {
		return ticks;
	}
	var date = new Date(parseInt(ticks / 10000) - 62135625600000);
	return utils.formatDate(date, format);
};

/**
 * 对MAC地址进行格式化，
 * @param {字符串} mac mac
 * @returns {String} 结果
 */
utils.formatMac = function (mac) {
	if (!mac) {
		return "";
	}
	if (mac.length === 17) {
		return mac.toUpperCase();
	}
	return mac.replace(/[:\-\.]/g, '').replace(/(\w{2})(?=\w{2,}$)/g, '$1-').toUpperCase();
};

/**
 * 转换数字为人民币大写形式
 * 1409.50    ->  壹仟肆佰零玖元伍角
 * 6007.14    ->  陆仟零柒元壹角肆分
 * 1680.32    ->  壹仟陆佰捌拾元零叁角贰分 或者 壹仟陆佰捌拾元叁角贰分
 * 107000.53  ->  壹拾万柒仟元零伍角叁分 或者 壹拾万零柒仟元伍角叁分
 * 16409.02   ->  壹万陆仟肆佰零玖元零贰分
 * 325.04     ->  叁佰贰拾伍元零肆分
 * @param {数值} m 输入数字
 * @returns {String} 结果
 */
utils.formatRMB = function (m) {
	var fraction = ['角', '分'];
	var digit = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
	var unit = [['元', '万', '亿'], ['', '拾', '佰', '仟']];
	var head = m < 0 ? '负' : '';
	m = Math.abs(m);
	var s = '';
	var i;
	for (i = 0; i < fraction.length; i++) {
		s += (digit[Math.floor(m * 10 * Math.pow(10, i)) % 10] + fraction[i]).replace(/零./, '');
	}
	s = s || '整';
	m = Math.floor(m);

	for (i = 0; i < unit[0].length && m > 0; i++) {
		var p = '';
		for (var j = 0; j < unit[1].length && m > 0; j++) {
			p = digit[m % 10] + unit[1][j] + p;
			m = Math.floor(m / 10);
		}
		s = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][i] + s;
	}
	return head + s.replace(/(零.)*零元/, '元').replace(/(零.)+/g, '零').replace(/^整$/, '零元整');
};

/**
 * 转换文件大小为友好的可读形式
 * @param {数值或字符串} value 输入数字
 * @returns {String} 结果
 */
utils.formatSize = function (value) {
	var num = Number(value);
	if (num === 0) {
		return "0";
	}
	var k = 1024;
	var sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
	var i = Math.floor(Math.log(num) / Math.log(k));
	return (num / Math.pow(k, i)).toPrecision(3) + ' ' + sizes[i];
};

utils.formatEnum = function (val, enums) {
	if (!enums) {
		return val;
	}
	var itemVal = enums.find(function (item) {
		return item.value == val;
	});
	if (itemVal) {
		return itemVal.text;
	}
	return val || "";
};

//弹窗
utils.popup = function (url, width, height) {
	var screenWidth = window.innerWidth || (document.documentElement || document.body).clientWidth;
	var screenHeight = window.innerHeight || (document.documentElement || document.body).clientHeight;
	width = width || 505;
	height = height || 300;
	url = url + (url.indexOf("?") >= 0 ? "&" : "?") + "nc=" + new Date().getTime();
	window.open(url, 'Pop', 'width=' + width + ',height=' + height + ',top=' + (screenHeight - height) / 2 + ',left=' + (screenWidth - width) / 2 + ',resizable=1,scrollbars=1,location=no');
};
utils.layerOpen = function (content, width, opts) {
	var optsAll = Object.assign({}, {
		type: 1,
		title: false,
		closeBtn: 0,
		area: width || '450px',
		skin: 'layui-layer-nobg', //没有背景色
		shadeClose: false,
		content: $(content)
	}, opts);
	layer.open(optsAll);
};
utils.layerPop = function (url, width, height, elementAt, opts) {
	var offset = 'auto';
	if (elementAt) {
		var $input = $(elementAt);
		var srcPos = $input.offset();
		offset = [(srcPos.top + $input.height() + 8 - $(document).scrollTop()) + "px", (srcPos.left - $(document).scrollLeft()) + "px"];
	}
	var cb = /(\?|&)callback=([^&]+)|$/.exec(url)[2];
	var optsAll = Object.assign({}, {
		type: 2,
		area: [width, height],
		fixed: true, //涓嶅浐瀹�
		closeBtn: 1,
		icon: 2,   //鍔犺浇鐨刬con绫诲瀷
		offset: offset,
		content: url,
		cancel: function (index, layero) {
			if (cb && parent[cb]) {
				parent[cb](index);
			}
		}
	}, opts);
	layer.open(optsAll);
};
utils.layerFull = function (url, title, id, opts) {
	if (!url) {
		return;
	}
	var cb = /(\?|&)callback=([^&]+)|$/.exec(url)[2];
	var optsAll = Object.assign({}, {
		id: id || "",
		title: title || "",
		type: 2,
		fixed: true,
		closeBtn: 1,
		icon: 2,   //加载的icon类型
		content: url,
		cancel: function (index, layero) {
			if (cb && parent[cb]) {
				parent[cb](index);
			}
		}
	}, opts);
	var popLayerIndex = layer.open(optsAll);
	layer.full(popLayerIndex);
};
//
function togglePanel(srcObj, layerID, dstPos, showOnly) {
	var $layer = $('#' + layerID);
	if ($layer.length === 0) {
		return;
	}
	if ($layer.is(':visible') && !showOnly) {
		$layer.css("display", "none");
		return;
	}
	var $srcObj = $(srcObj);
	var srcOffset = $srcObj.offset();
	var srcX = srcOffset.left;
	var srcY = srcOffset.top;
	var srcW = $srcObj.outerWidth(true);
	var srcH = $srcObj.outerHeight(true);
	var divWidth = parseInt($layer.css('width'));
	var dstX;
	var dstY = srcY + srcH;
	if (dstPos == "left") {
		dstX = srcX - divWidth;
	} else if (dstPos == "center") {
		if (divWidth > srcW) {
			dstX = srcX + srcW / 2 + divWidth / 2;
			if (dstX > document.body.clientWidth) {
				dstX = srcX - (divWidth - srcW);
			} else {
				dstX = srcX - (divWidth / 2 - srcW / 2);
			}
		} else {
			dstX = srcX + srcW / 2 - divWidth / 2;
		}
	} else {
		if (divWidth > srcW) {
			dstX = srcX + divWidth;
			if (dstX > document.body.clientWidth) {
				dstX = srcX - divWidth;
			} else {
				dstX = srcX;
			}
		} else {
			dstX = srcX;
		}
	}
	$layer.css('left', dstX + "px");
	$layer.css("position", "absolute");
	$layer.css('top', dstY + "px");
	$layer.css('display', 'block');
}
/*在目标元素下方中间显示*/
function togglePanelC(srcObj, layerID, showOnly) {
	togglePanel(srcObj, layerID, "center", showOnly);
}
/*元素的右上角显示在目标元素的左下角*/
function togglePanelL(srcObj, layerID, showOnly) {
	togglePanel(srcObj, layerID, "left", showOnly);
}
/*在目标元素左下角显示*/
function togglePanelR(srcObj, layerID, showOnly) {
	togglePanel(srcObj, layerID, "right", showOnly);
}
function hidePanel(layerID) {
	$('#' + layerID).hide();
}
function showPanel(layerID) {
	var $layer = $('#' + layerID);
	if ($layer.length === 0) {
		return;
	}
	if ($layer.is(':visible')) {
		return;
	}
	$layer.css("display", "block");
}
function viewDetailLog(tid, mid) {
	utils.layerFull(globalData.baseUrl + "/sys/tablelog/list?tid=" + tid + "&mid=" + mid, '查看操作日志', 'SysTableLog');
}
/**
 * 笛卡尔乘积（输入参数：多个数组）
 * */
function cartesianProduct() {
	return Array.prototype.reduce.call(arguments, function (a, b) {
		var ret = [];
		a.forEach(function (arr) {
			b.forEach(function (bval) {
				ret.push(arr.concat([bval]));
			});
		});
		return ret;
	}, [[]]);
}
/**
 * ie11不支持动态属性
 */
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
